package com.ny.controller;

import com.ny.entity.Reservations;
import com.ny.service.ClassroomService;
import com.ny.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private ReservationService reservationService;
    @Autowired
    private ClassroomService classroomService;
    @RequestMapping("adminLogin")
    public String adminLogin(Model model){
        List<Reservations> reservationList = reservationService.findAllReservations();
        model.addAttribute("reservationList", reservationList);
        return "reservations";
    }
    @GetMapping("/approveReservation")
    public String approveReservation(@RequestParam("reservationId") int reservationId) {
        reservationService.updateReservationStatus(reservationId, "已通过", null);

        int classroomId = reservationService.getReservationById(reservationId);
        classroomService.updateClassroomStatusById(classroomId, "已预约");
        return "redirect:/reservations";
    }
//不通过
    @PostMapping("/rejectReservation")
    public String rejectReservation(@RequestParam("reservationId") int reservationId, @RequestParam("rejectReason") String rejectReason) {
        reservationService.updateReservationStatus(reservationId, "不通过", rejectReason);
        int classroomId = reservationService.getReservationById(reservationId);
        classroomService.updateClassroomStatusById(classroomId, "不通过");
        return "redirect:/reservations";
    }
}
